import api from '../../api/api.js'
import load from '../templates/loader/loader.js'//载入动画框
Page({

  /**
   * 页面的初始数据
   */
  data: {
    publicOrder: [],
    othersOrder: [],
    warehouseData: [],
    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    currentSecondTab_0: 0,//次级选项
    currentSecondTab_1: 0,//次级选项
    activeList_0: [true, false, false],
    activeList_1: [true, false, false],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 100;
        var calc2 = clientHeight * rpxR - 200;
        that.setData({
          winHeight: calc,
          winHeight2: calc2
        });
      }
    });
    api.getUser({
      done: (user) => {
        that.data.userInfo = user
        that.getWareHouse()
      }
    })
  },
  getWareHouse: function () {
    var that = this
    var json = '{"REQ_HEAD":{"SIGN":"null"},"REQ_BODY":{"vendor_id":"' + that.data.userInfo.id + '"}}'
    api.getWareHouse({
      method: 'POST',
      data: { REQ_MESSAGE: json },
      success: (res) => {
        var bigType = [], smallType = []
        if (res.rspcod == "200") {
          res.rows.forEach(function (item) {
            bigType.push(item.bigType)
            smallType.push(item.smallType)
          });
          bigType = bigType.join("、")
          smallType = smallType.join("、")
          that.setData({
            warehouseData: res.rows,
            bigType: bigType,
            smallType: smallType
          })
        }
      }
    })
  },
  switchTab: function (e) {
    console.log(e);
    this.setData({
      currentTab: e.detail.current
    });
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTab == cur) { return false; }
    else {
      if (cur == 1) {
        if (this.data.publicOrder.length <= 0) {
          var data = {
            current: this.data.currentSecondTab_0,
            crowdstatus: 1,
            orderstatus: 1,
          }
          this.getManufactorOrder(data)
        }
      } else if (cur == 2) {
        if (this.data.othersOrder.length <= 0) {
          var data = {
            current: this.data.currentSecondTab_1,
            orderstatus: 0,
          }
          this.getOthersOrder(data)
        }
      }
      this.setData({
        currentTab: cur
      })
    }
  },
  swichSecondNav: function (e) {
    // var cur = e.target.dataset.current;
    // if (this.data.currentSecondTab == cur) {
    //   return false
    // } else {
    //   this.setData({
    //     currentSecondTab_0: cur
    //   })
    // }
    this.setSwichSecondNavActive(e);
  },
  setSwichSecondNavActive: function (e) {
    var cur = e.target.dataset;
    var tab = {};

    tab["currentSecondTab_" + cur.tab] = cur.current;

    if (this.data.currentSecondTab == cur) {
      return false;
    } else {
      this.setData(tab);
      if (!this.data["activeList_" + cur.tab][cur.current]) {
        this.data["activeList_" + cur.tab][cur.current] = true
        if (cur.tab == 0)
          this.getManufactorOrder(cur)
        else
          this.getOthersOrder(cur)
      }
    }
  },
  onScroll: function (e) {
    var data = e.currentTarget.dataset
    // this.getManufactorOrder(data.index)
  },
  getManufactorOrder: function (e) {
    var that = this
    var json = '{"REQ_HEAD":{"SIGN":"8495897b17c2813da1ef5e8c7ca45e21"},"REQ_BODY":{"vendorID":"' + that.data.userInfo.id + '","crowdStatus":"' + (e.crowdstatus || 1) + '","orderStatus":"' + (e.orderstatus || 1) + '","pSize":"15","pNum":"1"}}'
    if (parseInt(e.current) == 0 || parseInt(e.current) == 2) {
      console.log("------uuu-----");
      api.getManufactorOrder2({
        method: 'POST',
        data: { REQ_MESSAGE: json },
        success: (res) => {
          var data = res.rows
          var publicOrder = that.data.publicOrder;
          data.forEach(function (item) {
            if (item.start_time) {
              item.create_time = that.dateFtt("yyyy-MM-dd hh:mm:ss", new Date(item.create_time));
            }
          });
          !publicOrder[e.current] ? publicOrder[e.current] = data : publicOrder[e.current] = publicOrder[e.current].concat(data)
          that.setData({
            publicOrder: publicOrder
          })
         }
      })
      return;
    }

    api.getManufactorOrder({
      method: 'POST',
      data: { REQ_MESSAGE: json },
      success: (res) => {
        var data = res.rows
        var publicOrder = that.data.publicOrder;
        !publicOrder[e.current] ? publicOrder[e.current] = data : publicOrder[e.current] = publicOrder[e.current].concat(data)
        that.setData({
          publicOrder: publicOrder
        })
      }
    })
  },
  getOthersOrder: function (e) {
    var that = this
    var json = '{"REQ_HEAD":{"SIGN":"8495897b17c2813da1ef5e8c7ca45e21"},"REQ_BODY":{"vendorID":"' + that.data.userInfo.id + '","orderStatus":"' + e.orderstatus + '","pSize":"15","pNum":"1"}}'
    api.getOthersOrder({
      method: 'POST',
      data: { REQ_MESSAGE: json },
      success: (res) => {
        console.log("res", res)
        var data = res.rows
        var othersOrder = that.data.othersOrder
        !othersOrder[e.current] ? othersOrder[e.current] = data : othersOrder[e.current] = othersOrder[e.current].concat(data)
        that.setData({
          othersOrder: othersOrder
        })
      }
    })
  },
  dateFtt: function (fmt, date) {
    var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  }
})